/**
 * 定义分页查询模块
 */
(function(window, angular, undefined){

	var module = angular.module('water.common.dataPage',['water.common.dateUtil','waterCommon']);
	
	module.service('$dataPage',function($waterDataCommon,$dateUtil){
		
		this.datetimePage = function(option){
			var dataGetter = new datetimePageDataGetter(option);
			return dataGetter;
		}
		
		/**
		 * 日期时间分页构造器,返回日期分页器对象，提供上一页下一页的方法
		 * url 接口地址
		 * queryObj 查询参数
		 * onSuccess 查询回调
		 * startDateField查询参数中，开始时间的字段名称
		 * endDateField 查询参数中，结束时间的字段名称
		 * hourPrePage 每一页的小时数,默认2小时
		 */
		function datetimePageDataGetter (option){
			var me = this;
			var url = option.url;
			var queryObj = option.queryObj;
			var onSuccess = option.onSuccess;
			var startDateField = option.startDateField;
			var endDateField = option.endDateField;
			var hourPrePage = option.hourPrePage;
			if(!hourPrePage){
				hourPrePage = 2;
			}
			this._pageGenerater = getDatePageGenerater(queryObj[startDateField],queryObj[endDateField],hourPrePage);
			this._queryObj = queryObj;
			
			this.getData = function(queryObject){
				$waterDataCommon.post(url,queryObject).then(function(data){
					data.pageInfo = me._pageInfo;
					if(onSuccess){
						onSuccess(data);
					}
				});
			}
			
			this.nextPage = function(){
				me._pageInfo = me._pageGenerater.nextPage();
				me._queryObj.START =me._pageInfo.startDate;
				me._queryObj.END = me._pageInfo.endDate;
				me.getData(me._queryObj);
			}
			this.prePage = function(){
				me._pageInfo = me._pageGenerater.prePage();
				me._queryObj.START =me._pageInfo.startDate;
				me._queryObj.END = me._pageInfo.endDate;
				me.getData(this._queryObj);
			}
			this.firstPage = function(){
				me._pageInfo = me._pageGenerater.firstPage();
				me._queryObj.START =me._pageInfo.startDate;
				me._queryObj.END = me._pageInfo.endDate;
				me.getData(me._queryObj);
			}
			this.lastPage = function(){
				me._pageInfo = me._pageGenerater.lastPage();
				me._queryObj.START =me._pageInfo.startDate;
				me._queryObj.END = me._pageInfo.endDate;
				me.getData(me._queryObj);
			}
		}
		
		/**
		 * 日期分页生成器
		 * 传入两个时间，时间间隔，跳转到的页数，返回一个分页结果对象
		 * startDateStr 开始时间 2017/01/05 16:10:58或者2017-01-05 16:11:18
		 * endDateStr 结束时间
		 * timeInterval 时间间隔，单位为小时
		 * sortType 排序方式，desc表示倒叙，asc表示顺序
		 */
		function getDatePageGenerater(startDateStr,endDateStr,timeInterval,sortType){
			if(!sortType){
				sortType = "desc";
			}
			var pageInfo = new DatePageGenerater(startDateStr,endDateStr,timeInterval,sortType,this);
			return pageInfo;
		}
		
		function DatePageGenerater(startDateStr,endDateStr,timeInterval,sortType){
			var me = this;
			me.startDate = $dateUtil.createDate(startDateStr);
			me.endDate = $dateUtil.createDate(endDateStr);
			me.currentStartDate = $dateUtil.createDate(startDateStr);
			me.currentEndDate = $dateUtil.createDate(endDateStr);
			me.timeDiff = (me.endDate - me.startDate) / 1000 / 60 / 60 ;
			me.pageCount = Math.ceil(me.timeDiff / timeInterval);
			me.pageIndex = 0;
			
			if(me.pageCount < 1){
				me.pageCount = 1;
			}
			
			this.prePage = function(){
				me.pageIndex--;
				var pageInfo = me._getPageInfo();
				return pageInfo;
			}
			
			this.nextPage = function(){
				me.pageIndex++;
				var pageInfo = me._getPageInfo();
				return pageInfo;
			}
			
			this.firstPage = function(){
				me.pageIndex = 1;
				var pageInfo = me._getPageInfo();
				return pageInfo;
			}
			
			this.lastPage = function(){
				me.pageIndex = me.pageCount;
				var pageInfo = me._getPageInfo();
				return pageInfo;
			}
			
			this._getPageInfo = function(){
				
				if(me.pageCount == 1){
					me.pageIndex = 1;
					var pageInfo = {
							startDate:$dateUtil.formatDate(me.startDate),
							endDate:$dateUtil.formatDate(me.endDate),
							pageCount:me.pageCount,
							pageIndex:me.pageIndex,
							timeInterval:timeInterval
					}
					return pageInfo;
				}
				else{
					if(me.pageIndex > me.pageCount){
						me.pageIndex = me.pageCount;
					}
					else if(me.pageIndex < 1){
						me.pageIndex = 1;
					}
					var hour = (me.pageIndex - 1) * timeInterval;
					if(sortType == "desc"){
						me.currentEndDate = $dateUtil.addHour(me.endDate,hour * -1);
						me.currentStartDate = $dateUtil.addHour(me.currentEndDate,timeInterval * -1);
					}
					else{
						me.currentStartDate = $dateUtil.addHour(me.startDate,hour);
						me.currentEndDate = $dateUtil.addHour(me.currentStartDate,timeInterval);
					}
					
					if(me.currentEndDate > me.endDate){
						me.currentEndDate = me.endDate
					}
					if(me.currentStartDate < me.startDate){
						me.currentStartDate = me.startDate
					}
					var pageInfo = {
							startDate:$dateUtil.formatDate(me.currentStartDate),
							endDate:$dateUtil.formatDate(me.currentEndDate),
							pageCount:me.pageCount,
							pageIndex:me.pageIndex,
							timeInterval:timeInterval,
							sortType:sortType
					}
					return pageInfo;
				}
			}
		}
		
	});
	
}(window, angular));